package ui {
import core.Facade;

import events.DigLevelEvent;

import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.EventDispatcher;
import flash.events.MouseEvent;

import ru.cjay.loading.JSONLoader;

import ru.cjay.loading.ResourceManager;
import ru.cjay.loading.UnsafeConnection;

public class WinnerInfoPopup extends EventDispatcher {
    private static var _instance:WinnerInfoPopup;
    private static var allowInstantiation:Boolean;

    private var _window:MovieClip = null;
    private var _zone_id:int;
    private var _zone:DigLevel;

    public static function getInstance():WinnerInfoPopup {
        if (_instance == null) {
            allowInstantiation = true;
            _instance = new WinnerInfoPopup();
            allowInstantiation = false;
        }
        return _instance;
    }

    public function WinnerInfoPopup() {
        if (!allowInstantiation) {
            throw new Error("Error: Instantiation failed: Use SingletonDemo.getInstance() instead of new.");
        }
    }

    public function show(object:Object):void {
        Facade.getInstance().fadeIn();
        var st:Stage = Facade.getInstance().stage;
        if (_window != null) {
            if (st.contains(_window)) {
                st.removeChild(_window);
            }
        }
        _zone_id = object.zone_id;
        _zone = object as DigLevel;
        if (Facade.getInstance().id_vkontakte == object.winner.user_id) {
            _window = new (ResourceManager.getInstance().getSwfAssetClass("ui:WinWindow") as Class)();

        } else {
            _window = new (ResourceManager.getInstance().getSwfAssetClass("ui:LooseWindow") as Class)();
            if (object.winner.fio != null) _window.winner_name.text = object.winner.fio;

        }
        st.addChild(_window);
        _window.time_end.text = object.winner.win_date;
        _window.jackpot_value.width = 180;
        _window.jackpot_value.text = object.jackpot_full;


        _window.prize_info.text = object.prize.name;
        _window.btn_ok.label.text = object.strings_quit_hint;

        _window.prize_icon.icon.addChild(UnsafeConnection.UnsafeLoader(object.prize.img,32,32));

        _window.btn_ok.addEventListener(MouseEvent.CLICK, click_ok);
        _window.btn_ok.addEventListener(MouseEvent.ROLL_OUT, onButtonRollOut);
        _window.btn_ok.addEventListener(MouseEvent.ROLL_OVER, onButtonRollIn);
        _window.btn_ok.gotoAndStop(1);
        _window.x = (Facade.WIDTH) / 2;
        _window.y = (Facade.HEIGHT) / 2;
    }

    private function click_ok(event:MouseEvent):void {
        var url:String = "http://vd.osa-corp.ru/map/quitgame?" + Facade.getInstance().authUrlParams() + "&zone_id=" + _zone_id;
        var loader:JSONLoader = new JSONLoader();
        loader.loadJSON(url, quit_zone);
    }

    private function onButtonRollOut(event:MouseEvent):void {
        _window.btn_ok.gotoAndStop(1);
    }

    private function onButtonRollIn(event:MouseEvent):void {
        _window.btn_ok.gotoAndStop(2);
    }

    private function quit_zone(object:Object):void {
        if (Facade.getInstance().stage.contains(_window)) {
            Facade.getInstance().stage.removeChild(_window);
        }
        _zone.dispatchEvent(new DigLevelEvent(DigLevelEvent.EXIT_LEVEL));
    }
}
}
